<template>
    <div class="components_edit_modal">
        <el-dialog width="500px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="formInfo" :model="formInfo" :rules="formRules" label-width="95px" size="mini">

                <el-form-item label="相册分类：" prop="categoryId">
                    <el-select v-model="formInfo.categoryId" filterable placeholder="请选择相册分类">
                        <el-option
                            v-for="item in options.categoryOptions"
                            :key="item.dictValue"
                            :label="item.dictValue"
                            :value="item.dictValue">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="店铺名称：" prop="shopId">
                    <el-select v-model="formInfo.shopId" filterable placeholder="请选择店铺店铺">
                        <el-option
                            v-for="item in options.shops"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="用户名称：" prop="userId">
                    <el-select v-model="formInfo.userId" filterable remote reserve-keyword placeholder="请选择用户"
                    :remote-method="remoteMethod"
                    :loading="loading">
                        <el-option
                            v-for="item in options.userOptions"
                            :key="item.id"
                            :label="item.nickname"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="相册私密：" prop="isPrivate">
                    <el-switch
                    v-model="formInfo.isPrivate"
                    active-text="按月付费"
                    inactive-text="按年付费">
                    </el-switch>
                </el-form-item>

                <!--<el-form-item label="照片上传：" prop="url">
                    <el-input v-model.trim="formInfo.url" style="display:none;"></el-input>
                    <pl-upload
                        :postData="attachment"
                        :displaymode="'images'"
                        :imageOnly="false"
                        :buttonIds='{select:"selectfiles1",upload:"postfiles1",container:"container1"}'
                        @reloadFiles="reloadFiles" ></pl-upload>
                </el-form-item>-->

                <el-form-item label="照片描述：" prop="description">
                    <el-input type="textarea" v-model.trim="formInfo.description" placeholder="请输入奖品描述"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addInfo('formInfo')">确 定</el-button>
                <el-button @click="state = false" size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    import { mapGetters } from 'vuex'
    import PlUpload from "../../../common/PlUpload"
    export default {
        components: {
            ...mapGetters({
                stat: 'getUserList_No',
            }),
            "pl-upload":PlUpload
        },
        data() {
            return {
                state: false,
                title: "",
                type: "",
                imageUrl: "",
                loading: false,
                attachment: [], //上传附件
                value: "",
                formInfo: {
                    albumId: null,
                    userId: null,
                    topicId: null,
                    url: null,
                    description: null
                },
                formRules: {
                    name: [{
                        required: true,
                        message: "请输入奖品名称",
                        trigger: "blur"
                    }],
                    awardType: [{
                        required: true,
                        message: "请输入字典名称",
                        trigger: "blur"
                    }],
                    chance: [{
                        required: true,
                        message: "请输入中奖概率",
                        trigger: "blur"
                    }],
                    amount: [{
                        required: true,
                        message: "请输入奖品金额",
                        trigger: "blur"
                    }],
                    description: [{
                        required: true,
                        message: "请输入奖品描述",
                        trigger: "blur"
                    }]
                },
                options:{
                    shops:[],
                    albumOptions:[],
                    topicOptions:[],
                    userOptions:[]
                },
                attachment:[],//上传附件
            };
        },
        created() {
            // this.getUserList();
            this.getCategoryList();
            this.getShopList();
        },
        mounted() {},
        methods: {
            showDialog(val) {
                // console.log(val);
                if (this.$refs["formInfo"] !== undefined) {
                    this.$refs["formInfo"].resetFields();
                }
                if (val) {
                    for (var key in val) {
                        this.formInfo[key] = val[key];
                    }
                    this.title = "编辑相册";
                    this.type = "edit";
                } else {
                    this.title = "添加相册";
                    this.type = "add";
                }
                this.state = true;
            },
            //  用户信息异步检索
            remoteMethod(query) {
                if (query !== '' && query.length>2 ) {
                    this.loading = true;
                    setTimeout(() => {
                        this.loading = false;
                        this.getUserList(query);
                    }, 200);
                } else {
                    this.options4 = [];
                }
            },
            // 获取相册分类
            getCategoryList() {
                let vm = this;
                vm.axios.get(api.dictionary.dictList+"?page=1&type=ALBUM").then((response) =>{
                    if (response.data.code == 200) {
                        vm.options.categoryOptions = response.data.data.dataList;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            // 获取店铺信息
            // 获取店铺数据
            getShopList() {
                let vm = this;
                vm.axios.get(api.shopsManagement.shopsList_No).then((response) =>{
                    if (response.data.code == 200) {
                        vm.options.shops = response.data.data;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            // 获取用户数据
            getUserList(name) {
                let vm = this;
                vm.axios.get(api.userManagement.userList_no + "?keyWord=" + name).then((response) =>{
                    if (response.data.code == 200) {
                        vm.options.userOptions = response.data.data;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            addInfo() {
                let vm = this;
                vm.$refs.formInfo.validate(valid => {
                    if (valid) {
                        let submission = ""; //提交方式
                        //选择提交方式
                        if (vm.type == "edit") {
                            submission = "put";
                        } else {
                            submission = "post";
                        }
                        // 提交
                        this.axios({
                            method: submission,
                            url: api.photosManagement.photosList,
                            data: vm.formInfo
                        }).then(response => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.tableList.$emit("vuetable:refresh");
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            reloadFiles: function (files) {
                let vm = this;
                this.attachment = [];

                files.forEach((item,idnex)=>{
                    item.url = item.fileStoreName;
                    this.attachment.push(item)
                })
                this.film.url = this.attachment[0].url;
            },
            // 格式化 url 地址
            urlFormat(strUrl){
                // var strUrl = "https://jstn-pic.oss-cn-qingdao.aliyuncs.com/d16e8272-4ac7-de6a-1071-a79ded59d29a.jpg"
                var arrUrl = strUrl.split("//")[1].split("/");
                var objectKey = arrUrl[1];
                var bucket = arrUrl[0].split(".")[0];
                var fileType = arrUrl[1].split(".")[1];
                var resultObject ={
                    bucket : bucket,
                    objectKey : objectKey,
                    fileType : fileType
                }
                return resultObject
            }
        }
    };
</script>
<style scoped>

</style>